package com.dengyuanke.upms.logic.controller;


import com.dengyuanke.common.core.result.R;
import com.dengyuanke.common.log.annotation.SysLog;
import com.dengyuanke.upms.domain.entity.Dept;
import com.dengyuanke.upms.logic.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 部门管理 前端控制器
 * </p>
 *
 * @author dengyuanke
 * @since 2019-06-29
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptService deptService;

    /**
     * 通过ID查询
     *
     * @param id ID
     * @return SysDept
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        return R.success(deptService.getById(id));
    }


    /**
     * 添加
     *
     * @param dept 实体
     * @return success/false
     */
    @SysLog("添加部门")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_add')")
    public R save(@Valid @RequestBody Dept dept) {
        return R.success(deptService.save(dept));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return success/false
     */
    @SysLog("删除部门")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_dept_del')")
    public R removeById(@PathVariable Integer id) {
        return R.success(deptService.removeById(id));
    }

    /**
     * 编辑
     *
     * @param dept 实体
     * @return success/false
     */
    @SysLog("编辑部门")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_dept_edit')")
    public R update(@Valid @RequestBody Dept dept) {
        return R.success(deptService.updateById(dept));
    }

}

